package no.ffi.tpm.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @author anders
 */
public class CaServiceTest {
    
    public static final String serviceURLAIK = "http://193.156.33.217:8084/IdmServlets/Ca";
    public static final String serviceURLIdP = "http://193.156.33.217:8084/IdmServlets/IdP";
   
    public static Object toPCA(Object Req,String url) throws Exception {  
       
        
        Object o2 = goHTTP(url,Req);
        //System.out.print(o2);
        return o2;
    }
    
    private static Object goHTTP(String url, Object o) throws IOException,ClassNotFoundException {
        URL serviceUrl = new URL(url);
        HttpURLConnection urlc = (HttpURLConnection)serviceUrl.openConnection();

        urlc.setRequestMethod("POST");
        urlc.setDoInput(true);
        urlc.setDoOutput(true);
        urlc.setRequestProperty("Content-Type", "application/x-javaobject");
        OutputStream os = urlc.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(o);
        oos.close();
        // Now wait for response
        // Error/timeout conditions throws a SocketException
        InputStream is = urlc.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        Object o2 = ois.readObject();
        System.out.println("Received object "+o2.toString());
        return o2;
    }
    
    

    
    
}
